//
//  MenuViewController.m
//  bApp
//
//  Created by Guglielmo Buzzelli on 20/05/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MenuViewController.h"

@interface MenuViewController ()

@end

@implementation MenuViewController
@synthesize backgroundImageView;
@synthesize menuScrollView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self start];
    
    
    NSDictionary *info = [json objectAtIndex:0];
    
    backgroundImageView.image = [UIImage imageNamed:@"menu_giorno_app@2x.png"];
    
    //R: 107, G: 55, B: 15 
    CGFloat dataRed=107.0/255.0;
    CGFloat dataBlue=15/255.0;
    CGFloat dataGreen=55.0/255.0;
    UIColor *brownMenuColor=[[UIColor alloc]initWithRed:dataRed green:dataGreen blue:dataBlue alpha:1];
    
    
    UILabel *header = [[UILabel alloc] initWithFrame:CGRectMake((backgroundImageView.center.x/2) +10, 45, 300, 20)];
    header.text = @"Menu del giorno";
    header.backgroundColor = [UIColor clearColor];
    header.font = [UIFont fontWithName:@"Zapfino" size:14.0];
    header.textColor = brownMenuColor;
    
    
    
    /*Formatto data*/
    if([info objectForKey:@"data_inserimento"]!=nil){
        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString *newstr = [NSString stringWithFormat:@"%@",[info objectForKey:@"data_inserimento"]];
        NSDate* sourceDate = [formatter dateFromString:newstr];
    
        NSString *dateStr;
        [formatter setDateFormat:@"EEEE d MMMM"];
        [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"it_IT"]];
        dateStr = [formatter stringFromDate:sourceDate]; 
    
        UILabel *header_data = [[UILabel alloc] initWithFrame:CGRectMake(backgroundImageView.center.x/2, 75, 300, 20)];
        header_data.text = dateStr;
        header_data.backgroundColor = [UIColor clearColor];
        header_data.font = [UIFont fontWithName:@"Zapfino" size:13.0];
        header_data.textColor = brownMenuColor;        
        [backgroundImageView addSubview:header_data];
    }
    
    [backgroundImageView addSubview:header];
    
    NSString  *piatto2, *piatto3, *piatto4, *piatto5, *piatto6, *piatto7 = nil;
    NSString *piatto1;
    
    if([info objectForKey:@"primo_primo"]){
        piatto1 = [NSString stringWithFormat:@"%@",[info objectForKey:@"primo_primo"]];
    }
    if([info objectForKey:@"secondo_primo"]){
         piatto2 =[NSString stringWithFormat:@"%@",[info objectForKey:@"secondo_primo"]];       
    }
    if([info objectForKey:@"primo_secondo"]){
         piatto3 = [NSString stringWithFormat:@"%@",[info objectForKey:@"primo_secondo"]];       
    }
    if([info objectForKey:@"secondo_secondo"]){
         piatto4 = [NSString stringWithFormat:@"%@",[info objectForKey:@"secondo_secondo"]];      
    }
    if([info objectForKey:@"primo_contorno"]){
         piatto5 = [NSString stringWithFormat:@"%@",[info objectForKey:@"primo_contorno"]];         
    }
    if([info objectForKey:@"secondo_contorno"]){
         piatto6 = [NSString stringWithFormat:@"%@",[info objectForKey:@"secondo_contorno"]];        
    }
    if([info objectForKey:@"terzo_contorno"]){
         piatto7 = [NSString stringWithFormat:@"%@",[info objectForKey:@"terzo_contorno"]];        
    }

    NSMutableArray *piatti = [NSMutableArray arrayWithObjects:piatto1,piatto2,piatto3,piatto4,piatto5,piatto6,piatto7, nil];
    
    int i=0;
    for(NSString * piatto in piatti){
        if(piatto !=nil){
            UITextView *secondoPiatto = [[UITextView alloc]initWithFrame:CGRectMake(10, 50 * i, 300, 50)];
            secondoPiatto.text=piatto;
            secondoPiatto.backgroundColor = [UIColor clearColor];
            //secondoPiatto.textColor = textColor;
            secondoPiatto.editable = NO;
            secondoPiatto.font = [UIFont fontWithName:@"Helvetica" size:13.0];
            secondoPiatto.textColor = brownMenuColor;
            
            [menuScrollView addSubview:secondoPiatto];
            i++;
        }   
    }
    
    menuScrollView.contentSize = CGSizeMake( menuScrollView.frame.size.width , 50 *piatti.count);    
    
    UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back_ground_app@2x.png"]];    
    [self.view addSubview:backgroundImage ];
    [self.view sendSubviewToBack:backgroundImage ];
    
    
}


-(void)getData:(NSData *) data{
    
    NSError *error;
    
    if(data!=nil)
        json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error ];
    
    
}

-(void) start{
    
    NSURL *url = [NSURL URLWithString:kGETUrlMap];
    
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    [self getData:data];
    
}

-(NSString *)getNewMenu:(NSString *)newMenuData{
    
    [self start];
    
    oldMenuDate = nil;
    [self start];
    NSDictionary *info = [json objectAtIndex:0];
    
    if(newMenuData!=nil){
        
        if (![newMenuData isEqualToString:[info objectForKey:@"data_inserimento"]]) {
            
            NSLog(@"MENU vecchio:%@ | attuale:%@",newMenuData,[info objectForKey:@"data_inserimento"]); 
            
            newMenuData = [info objectForKey:@"data_inserimento"];
            
            UILocalNotification *notifica = [[UILocalNotification alloc] init];
            
            NSDate *startDate = [[NSDate date] dateByAddingTimeInterval:30];
            
            [notifica setFireDate:startDate];
            [notifica setAlertBody:@"Nuovo menu del giorno!"];
            [notifica setAlertAction:@"Scopri le novità"];
            
            [[UIApplication sharedApplication] scheduleLocalNotification:notifica ];
            [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
            
            
        }else{
            //[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
        }
        
    }else{
        newMenuData = [info objectForKey:@"data_inserimento"];  
       // [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    }
    
    
    return newMenuData;
}

- (void)viewDidUnload
{
    [self setBackgroundImageView:nil];
    [self setMenuScrollView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
